<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visible && !detailInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
          :filter-btn="filterInfo.btn"
          :clear-type="clearType"
        >
          <!-- <template v-slot:filter-goodsLocationId>
            <el-input
          v-model="filterInfo.query.locationNo"
          class="not-mongolia"
          type="input"
          :disabled="true"
          :placeholder="'请选择货位编号'"
        >
          <template #append>
            <el-icon @click="checkGoods"><MoreFilled /></el-icon>
          </template>
        </el-input>
          </template> -->
          <template v-slot:filter-operateTime>
            <el-date-picker
              v-model="operateTime"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              type="datetimerange"
              range-separator="至"
              align="right"
              start-placeholder="操作时间开始日期"
              end-placeholder="操作时间结束日期"
              @change="timeChange"
            ></el-date-picker>
          </template>
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <template v-slot:filter-del>
            <el-button :disabled="!canDelete" @click="del()">删除</el-button>
          </template>
        </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :tabIndex="true"
        :checkBox="false"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-orderNo="scope">
          <span class="slip-edit" @click="showDetail(scope.row)">{{
            scope.row.orderNo
          }}</span>
        </template>
      </TableContainer>
    </div>
    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :orderId="modifyInfo.id"
      @returnModify="getList"
    />
    <TransferOrderDetail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :table-row="detailInfo.data"
      @returnBack="getList"
    />
  </div>
</template>

<script setup name="cargoSpaceTransfer">
import Modify from './modify'
import TransferOrderDetail from './detail'

import {
  getTransferOrderPage,
  transferTransferOrder,
  delTransferOrder,
} from '@/api/wms'
import { getDepotAll } from '@/api/core'
const { proxy } = getCurrentInstance()

const operateTime = ref([])
// 过滤相关配置
const filterInfo = ref({
  query: {
    depotAuth: 1,
  },
  copyQuery: {},
  list: [
    {
      componentType: 'input',
      label: '单据号',
      value: 'orderNoLike',
      event: search,
    },
    {
      label: '转移仓库',
      value: 'storeDepotName',
      asValue: 'depotId',
      componentType: 'slip-select',
      query: {
        depotAuth: 'id',
        status: 1,
        useTypeId: '1',
        isCargoSpaceManage: 1,
      },
      api: getDepotAll,
      event: search,
    },
    {
      componentType: 'select-shelf-no',
      label: '转出货位编码',
      value: 'locationNo',
      asValue: 'goodsLocationId',
      checkBox: false,
      highlightRow: true,
      event: search,
    },
    {
      componentType: 'select-model',
      label: '转出货位类型',
      value: 'locationType',
      event: search,
      code: 'goods_location_type',
    },
    {
      componentType: 'select-model',
      label: '状态',
      value: 'status',
      event: search,
      code: 'wms_transfer_order_status',
    },
    {
      componentType: 'select-user',
      label: '创建人',
      value: 'createUserName',
      asValue: 'createUser',
      event: search,
      checkBox: false,
      hidden: true,
      show: true,
      highlightRow: true,
    },
    {
      slot: true,
      label: '创建日期',
      value: 'operateTime',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '新增',
      event: create,
      show: proxy.$permission('cargoSpaceTransfer:add'),
    },
  ],
})
const clearType = ref(true)
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getTransferOrderPage,
  data: [],
  selections: [],
  fieldList: [
    { label: '单据号', value: 'orderNo', type: 'slot' },
    { label: '转移仓库', value: 'depotName' },
    { label: '转出货位编码', value: 'locationNo' },
    { label: '转出货位类型', value: 'locationTypeValue' },
    { label: '转移品类数', value: 'transferVarietiesQty' },
    { label: '转移总个数', value: 'transferQty' },
    { label: '状态', value: 'statusValue' },
    { label: '备注', value: 'remark' },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '120',
    btList: [
      {
        label: '转移',
        event: transfer,
        popconfirmText: '请确认是否继续转移数据？',
        show: proxy.$permission('cargoSpaceTransfer:transfer'),
        ifRender: function (row) {
          return row.status !== 2
        },
      },
      {
        label: '修改',
        event: update,
        show: proxy.$permission('cargoSpaceTransfer:update'),
        ifRender: function (row) {
          return row.status !== 2
        },
      },
      {
        label: '删除',
        event: del,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('cargoSpaceTransfer:del'),
        ifRender: function (row) {
          return row.status !== 2
        },
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
})
const detailInfo = ref({
  visible: false,
  data: {},
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.startDate = val[0]
    filterInfo.value.query.endDate = val[1]
  } else {
    filterInfo.value.query.startDate = ''
    filterInfo.value.query.endDate = ''
  }
  search()
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  operateTime.value = []
  clearType.value = !clearType.value
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//展示明细
function showDetail(row) {
  detailInfo.value.data = row
  detailInfo.value.visible = true
}
//创建
function create() {
  modifyInfo.value.title = '新增货位转移'
  modifyInfo.value.id = ''
  modifyInfo.value.visible = true
}
function transfer(row = undefined) {
  transferTransferOrder({
    id: row.id,
  }).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function update(row) {
  //修改
  modifyInfo.value.id = row.id
  modifyInfo.value.title = '修改货位转移'
  modifyInfo.value.visible = true
}
//删除
function del(row = undefined) {
  const ids = _getIds(row)
  delTransferOrder(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function _getIds(row) {
  let ids = []
  if (row && row.id) {
    ids = [row.id]
  } else {
    if (tableInfo.value.selections.length > 0) {
      ids = tableInfo.value.selections.map((row) => row.id)
    }
  }
  return ids
}
</script>

<style scoped></style>
